package com.example.bluetoothble;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.polidea.rxandroidble2.RxBleDevice;
import com.polidea.rxandroidble2.scan.ScanResult;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

class ScanResultsAdapter extends RecyclerView.Adapter<ScanResultsAdapter.ViewHolder> {

  static class ViewHolder extends RecyclerView.ViewHolder {

    @BindView(android.R.id.text1) TextView line1;
    @BindView(android.R.id.text2) TextView line2;

    ViewHolder(View itemView) {
      super(itemView);
      ButterKnife.bind(this, itemView);
    }
  }

  interface OnAdapterItemClickListener {

    void onAdapterViewClick(View view);
  }

  private static final Comparator<ScanResult> SORTING_COMPARATOR = (lhs, rhs) -> lhs.getBleDevice()
      .getMacAddress()
      .compareTo(rhs.getBleDevice().getMacAddress());
  private final List<ScanResult> data = new ArrayList<>();
  private OnAdapterItemClickListener onAdapterItemClickListener;
  private final View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override public void onClick(View v) {

      if (onAdapterItemClickListener != null) {
        onAdapterItemClickListener.onAdapterViewClick(v);
      }
    }
  };

  void addScanResult(ScanResult bleScanResult) {
    // Not the best way to ensure distinct devices, just for sake on the demo.

    for (int i = 0; i < data.size(); i++) {

      if (data.get(i).getBleDevice().equals(bleScanResult.getBleDevice())) {
        data.set(i, bleScanResult);
        notifyItemChanged(i);
        return;
      }
    }

    data.add(bleScanResult);
    Collections.sort(data, SORTING_COMPARATOR);
    notifyDataSetChanged();
  }

  void clearScanResults() {
    data.clear();
    notifyDataSetChanged();
  }

  ScanResult getItemAtPosition(int childAdapterPosition) {
    return data.get(childAdapterPosition);
  }

  @Override public int getItemCount() {
    return data.size();
  }

  @Override public void onBindViewHolder(ViewHolder holder, int position) {
    final ScanResult rxBleScanResult = data.get(position);
    final RxBleDevice bleDevice = rxBleScanResult.getBleDevice();
    holder.line1.setText(String.format(Locale.getDefault(), "%s (%s)", bleDevice.getMacAddress(),
        URLEncoder.encode(bleDevice.getName())));
    holder.line2.setText(String.format(Locale.getDefault(), "RSSI: %d", rxBleScanResult.getRssi()));
  }

  @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View itemView = LayoutInflater.from(parent.getContext())
        .inflate(android.R.layout.two_line_list_item, parent, false);
    itemView.setOnClickListener(onClickListener);
    return new ViewHolder(itemView);
  }

  void setOnAdapterItemClickListener(OnAdapterItemClickListener onAdapterItemClickListener) {
    this.onAdapterItemClickListener = onAdapterItemClickListener;
  }
}
